package top.huhuiyu.springboot2.util;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期处理的工具类
 *
 * @author 胡辉煜
 */
public class DateUtils {
  public static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  public static final DateTimeFormatter DTF_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");

  public static String format(Date date, boolean dateMode) {
    if (dateMode) {
      return DTF_DATE.format(date.toInstant().atZone(ZoneId.systemDefault()));
    }
    return DTF.format(date.toInstant().atZone(ZoneId.systemDefault()));
  }

  public static Date parse(String date) {
    try {
      return Date.from(LocalDateTime.parse(date, DTF).atZone(ZoneId.systemDefault()).toInstant());
    } catch (Exception ex) {
      return Date.from(LocalDateTime.parse(date + " 00:00:00", DTF).atZone(ZoneId.systemDefault()).toInstant());
    }


  }

//  data 数据 date 日期 from 来自 form 表单

}
